package com.qf.shop.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ESConfig {

    @Value("${es.host}")
    private String host;

    @Value("${es.port}")
    private Integer port;

    @Bean
    public RestHighLevelClient client(){

        // 1.设置es的ip和端口
        HttpHost httpHost = new HttpHost(host,port);

        // 2.把httpHost对象封装在RestClientBuilder
        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);

        // 3.把RestClientBuilder封装到RestHighLevelClient
        RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);

        // 4.返回
        return client;
    }
}
